--[[Camera类
摄像机类负责管理游戏中的摄像机位置和游戏中的3d处理
]]
Camera = {}

-- 把摄像机移到玩家位置
function Camera.setCameraToPlayer(a)
    local x = player.xo + (player.x - player.xo) * a
    local y = player.yo + (player.y - player.yo) * a
    local z = player.zo + (player.z - player.zo) * a
    local fov = GameSetting.Fov + player.fovOffsetO + (player.fovOffset - player.fovOffsetO) * a
    perspective(fov,WIDTH / HEIGHT,0.1,1000000)
    local m = viewMatrix()
    -- 第三人称视角处理
    if GameSetting.ThirdPersonView then
        m = m:translate(0,0,-3.5)
    end
    m = m:rotate(player.xRot,1,0,0)
    m = m:rotate(player.yRot,0,1,0)
    m = m:translate(-x,-y,-z)
    viewMatrix(m)
end

-- 视角摇晃
function Camera.shakeCamera(a)
    local wd = player.walkDist + (player.walkDist - player.walkDistO) * a
    local bob = player.bob + (player.bob - player.oBob) * a
    local tilt = player.tilt + (player.tilt - player.oTilt) * a

    local m = matrix()
    m = m:translate(math.sin(wd * math.pi) * bob * 0.25,-math.abs(math.cos(wd * math.pi) * bob)*0.5,0)
    m = m:rotate(math.sin(wd * math.pi) * bob * 2.5,0,0,1)
    m = m:rotate(math.abs(math.cos(wd * math.pi + 0.2) * bob) * 2,1,0,0)
    m = m:rotate(tilt,1,0,0)
    viewMatrix(m)
end

-- 获得摄像机位置
function Camera.getEyePositionVec3(a)
    return vec3(player.xo + (player.x - player.xo) * a,player.yo + (player.y - player.yo) * a,player.zo + (player.z - player.zo) * a)
end

-- ??什么鬼
function Camera.mouseRotateXY(delta, rotMatrix)
    -- Rotation without gimbal lock by only multiplying delta rotations
    -- to a matrix. Pass delta vec2 (x and y) and current rotation matrix. New
    -- rotation matrix is returned.
    local rm = matrix()
    rm = rm:rotate(delta.x, 0, 1, 0)
    rm = rm:rotate(-delta.y, 1, 0, 0)
    return rotMatrix * rm
end